package com.kcht.np.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcht.np.common.exception.ParameterException;
import com.kcht.np.entity.SysDepartment;
import com.kcht.np.constant.InitData;
import com.kcht.np.mapper.SysDepartmentMapper;
import com.kcht.np.service.SysDepartmentService;
import com.kcht.np.vo.dept.DeptCanCreateInfo;
import com.kcht.np.vo.dept.DeptCanModifyInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
  * <p>
  * 部门 Service 实现类
  * </p>
  *
  * @Package:        com.kcht.np.mp.service.impl
  * @Description:    部门 Service 实现类
  * @Author:         liuqi
  * @CreateDate:     2020/3/31 21:24
 */

@Slf4j
@Service
@DS("kchtnp")
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeDeptById(Serializable id) {
        if(InitData.ROOT_DEPT.equals(id)||InitData.TOP_DEPT.equals(id)){
            throw new ParameterException("不能删除顶级部门");
        }
        if(baseMapper.selectById(id)==null){
            throw new ParameterException("无此部门");
        }
        if (baseMapper.countChild((Long) id) > 0) {
            throw new ParameterException("当前部门下，还有子部门，无法删除！");
        }
        if (baseMapper.countUserRef((Long) id) > 0) {
            throw new ParameterException("当前部门下，还有其用户在使用，无法删除！");
        }
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDept(DeptCanCreateInfo info) {
        if(baseMapper.countByDeptName(info.getName())==1){
            throw new ParameterException("已存在<"+info.getName()+">部门");
        }
        SysDepartment entity=new SysDepartment();
        if(InitData.TOP_DEPT.equals(info.getParentId())){
            entity.setLevel(0);
        }else{
            SysDepartment parent = baseMapper.selectById(info.getParentId());
            if(parent==null){
                throw new ParameterException("指定的父部门不存在");
            }
            entity.setLevel(parent.getLevel()+1);
        }
        entity.setName(info.getName());
        entity.setParentId(info.getParentId());
        entity.setDescription(info.getDescription());
        entity.setCreateTime(LocalDateTime.now());
        entity.setLastUpdateTime(LocalDateTime.now());
        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDept(DeptCanModifyInfo info) {
        if(InitData.ROOT_DEPT.equals(info.getDid())|| InitData.TOP_DEPT.equals(info.getDid())){
            throw new ParameterException("不能更新顶级部门");
        }
        if(baseMapper.selectById(info.getDid())==null){
            throw new ParameterException("指定的部门不存在");
        }
        Long pid=info.getParentId();
        if(InitData.TOP_DEPT.equals(pid)){
            info.setLevel(0);
        }else if(pid!=null){
            SysDepartment parent = baseMapper.selectById(pid);
            if(parent==null){
                throw new ParameterException("指定的父部门不存在");
            }
            info.setLevel(parent.getLevel()+1);
        }
        baseMapper.updateDept(info);
    }
}
